/*
 * @author rsundara.in
 * 
 * @version 1.2
 * 
 * Development Environment : JDeveloper 2.0 Name of the Application : PopulateTable.java Creation/Modification History :
 * 
 * rsundara.in 29-Dec-1998 Created neshastr 22-May-2002 Certified on Oracle9i JDeveloper Savitha 10-Jan-2003 Certified
 * on Linux Moved dbConnection() and loadparams() methods to Sample class.
 */
package oracle.otnsamples.jdbc.longraw;

// java sql packages
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.PreparedStatement;

// java io packages
import java.io.FileInputStream;
import java.io.File;

/**
 * This sample creates a table otn_airline_longraw in the database with 3 varchar2 columns and 1 long raw column. The
 * columns are code varchar2(2) name varchar2(40) partner varchar2(1) airline_logo long raw
 * 
 * After creating the table, it also populates data into the table
 * 
 * The following methods contain JDBC calls and encapsulate database access: createTable method: creates the
 * OTN_AIRLINE_LONGRAW insertData method: inserts a row into the OTN_AIRLINE_LONGRAW
 * 
 */
public class PopulateTable {

    /** The Database connection object */
    private Connection connection;

    /** create sample frame object to redirect error messages */
    private LongRawFrame gui = null;

    /**
     * Constructor. Instantiates connection and gui.
     */
    public PopulateTable(Connection conn, LongRawFrame gui) {
        this.connection = conn;
        this.gui = gui;
    }

    /**
     * Main entry point for the class. Sets up the database connection and creates the OTN_AIRLINE_LONGRAW table, and
     * populates it.
     */
    public void createSchemaTable() {

        // Create the OTN_AIRLINE_LONGRAW table
        createTable();

        try {
            // Insert rows into the table
            insertData("A1", "WindAirways", "I", "WindAirways.gif");
            insertData("A2", "GreenLableAirways", "B", "GreenLableAirways.gif");
            insertData("A3", "FlyHighAirways", "I", "FlyHighAirways.gif");
            insertData("A4", "TopSurferAirlines", "B", "TopSurferAirlines.gif");
            insertData("A5", "JumboAirways", "B", "JumboAirways.gif");
        } catch (Exception ex) {
            gui.putStatus("Error in inserting data. " + ex.toString());
        }
    }

    /**
     * This method creates the table OTN_AIRLINE_LONGRAW in the database
     */
    private void createTable() {
        try {
            // Statement object for executing the query
            Statement stmt = connection.createStatement();

            String query = "CREATE TABLE otn_airline_longraw (code VARCHAR2(2)," + "name VARCHAR2(40)," + "partner VARCHAR2(1),"
                    + "airline_logo LONG RAW)";
            stmt.executeQuery(query); // Execute the statement
            stmt.close();
        } catch (SQLException ex) {
            gui.putStatus("Error creating table. " + ex.toString());
        }
    }

    /**
     * This method inserts contents of the specified file into the AIRLINE_LOGO column in the OTN_AIRLINE_LONGRAW
     */
    private void insertData(String code, String name, String partner, String logoname) throws Exception {
        try {
            // Create a File to read data from the specified text file (logoname)
            // in 'images' directory
            String fileName = "F:\\downloads\\LongRawSample\\LongRawSample" + File.separator + "images" + File.separator
                    + logoname;
            File file = new File(fileName);

            // Prepare the statement for inserting a row into the OTN_AIRLINE_LONGRAW
            PreparedStatement pstmt = connection
                    .prepareStatement("INSERT INTO otn_airline_longraw(Code,Name,Partner,Airline_Logo) " + "VALUES(?, ?, ?, ?)");

            // Bind the parameter values for the above statement
            pstmt.setString(1, code); // Bind CODE
            pstmt.setString(2, name); // Bind NAME
            pstmt.setString(3, partner); // Bind PARTNER

            // Bind the AIRLINE_LOGO column to an input stream that returns the
            // binary data to be inserted into the LONGRAW column
            pstmt.setBinaryStream(4, new FileInputStream(file), (int) file.length());
            pstmt.execute(); // Execute the statement
        } catch (Exception ex) {
            System.out.println("error in inserign data: " + ex.toString());
        }
    }

}
